<template>
  <div class='sub-category' v-if="sub">
    <div class="container">
      <!-- 面包屑 -->
      <SubBread />
      <!-- 筛选区 -->
      <SubFilter />
            <!-- 结果区域 -->
      <div class="goods-list">
        <!-- 排序 -->
        <SubSort />

        <!-- 加载 -->
         <XtxInfiniteLoading v-model="loading" :finished="finished" @load="getlist" >
           <!-- 商品列表 -->
        <ul class="list">
          <li v-for="i in list" :key="i.id">
            <GoodsItem :goods='i'></GoodsItem>
          </li>
        </ul>
         </XtxInfiniteLoading>
      </div>
    </div>
  </div>
</template>

<script>
import { provide, ref, watch, reactive } from 'vue'
import { useRoute } from 'vue-router'
import { findSubCategoryFilter, findSubCategoryGoods } from '@/api/category'
import SubBread from './components/sub-bread'
import SubFilter from './components/sub-filter.vue'
import SubSort from './components/sub-sort.vue'
import GoodsItem from './components/goods-item.vue'
export default {
  name: 'SubCategory',
  components: { SubBread, SubFilter, SubSort, GoodsItem },
  setup () {
    const route = useRoute()
    const sub = ref(null)
    // 加载状态
    const loading = ref(false)
    // 结束状态
    const finished = ref(false)
    const defaultParams = () => {
      return {
        categoryId: route.params.id,
        sortField: null, // 取值范围：[publishTime,orderNum,price,evaluateNum]
        sortMethod: null, // asc为正序，desc为倒序，默认为desc
        inventory: false,
        onlyDiscount: false,
        attrs: [],
        brandId: null,
        pageSize: 20,
        page: 1
      }
    }
    // 把page 提出来 不然会影响排序筛选 页面进来就加载数据 页码就加1  加1 就触发watch 又重置了页码 死循环
    const page = reactive({
      page: 1
    })
    watch(() => route.params.id, async (id) => {
      if (route.path === `/category/sub/${id}`) {
        const { result } = await findSubCategoryFilter(id)
        sub.value = result
        const obj = defaultParams() // 这里重新调用了 每次都是最新的id
        for (const key in obj) {
          sortParams[key] = obj[key]
        }
        list.value = []
      }
    }, { immediate: true })
    provide('subData', sub)
    const sortParams = reactive(defaultParams())
    provide('sortParams', sortParams)
    // console.log(sub)
    const list = ref([])
    const getlist = async () => {
      // console.log(2222)
      // console.log(sortParams)
      // console.log(page.value)
      loading.value = true
      const { result } = await findSubCategoryGoods({
        ...sortParams,
        ...page
      })
      loading.value = false
      list.value.push(...result.items)
      // 回来的数据小于页容量了 说明后面没有数据了
      page.page++
      // console.log(page.page)
      // result.items.length < sortParams.pageSize
      if (result.items.length < sortParams.pageSize) {
        // console.log(3333)
        finished.value = true
      }
    }
    // 筛选区 跟 排序区 切换的关键代码
    watch(sortParams, () => {
      // 大于1 因为getlist最后会页码+1 所以此时肯定已经有列表出现
      // 防止页面刚进入就把下面的列表加载了
      if (page.page > 1) {
        // console.log(1111)
        list.value = [] // 数组数据重置
        page.page = 1 // 切换筛选条件时 页数重置
        finished.value = false // 让分页重新开启
        getlist()
      }
    })
    return {
      sub, list, getlist, loading, finished
    }
  }
}
</script>

<style scoped lang="less">
.goods-list {
  background: #fff;
  padding: 0 25px;
  margin-top: 25px;
  ul {
    display: flex;
    flex-wrap: wrap;
    padding: 0 5px;
    li {
      margin-right: 20px;
      margin-bottom: 20px;
      &:nth-child(5n) {
        margin-right: 0;
      }
    }
  }
}
</style>
